﻿define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Afallseller, GoPage, GoDialog, $timeout, $stateParams, Auth, $window, $ionicScrollDelegate,$ionicSideMenuDelegate,$ionicModal) {
		$scope.input = new Object();
		$scope.input.kind = ""; //订单种类 1:普通 2:秒杀 3:团购 4:拍卖 5:限时抢购 6:积分兑换  新增:[7新品测试][8新品赠送][9新品广告]
		$scope.input.time = ""; 
		$scope.input.goods_bn = ""; //商品编号，没用到该参数
		$scope.input.order_id = ""; //订单id
		$scope.input.goods_name = ""; //商品名称
		$scope.input.nPage = 1;
		$scope.tPage = 0;
		$scope.$on('$ionicView.afterEnter', function() {
			/*
			var lan = 0; //这是状态栏，只有ios的APP才有
			if(window.cordova && window.cordova.plugins && ionic.Platform.isIOS()) {
				lan = 20;
			}
			//整体高度
			var all = $window.innerHeight;
			$scope.myObj = { //滚动条可视范围
				"height": all - 44*2 - lan + "px"
			};
			*/
			//动态设置高度
			GoPage.init_top($scope, 44,0,44);
			$scope.input.type = $stateParams.type; //默认全部。订单状态： ""=>全部  'shiped'=>已发货 'ship'=>待发货 'finish'=>已完成 'dead'=>已取消 'refunded'=>退款中 'refunded'=>待售后
			$scope.init_data();
		}, false);

		//数据初始化	
		$scope.init_data = function() {
			$scope.firstHide = false;
			Afallseller.get_list($scope.input.type, 1, $scope.input.kind, $scope.input.goods_bn, $scope.input.goods_name, $scope.input.order_id, $scope.input.time, function(data) {
				$scope.input.nPage = 1;
				$scope.datalist = data;
				if(data!=null){
					$scope.tPage = data.page.tPage;
				}
				$ionicScrollDelegate.scrollTop();//回到顶部
				$timeout(function() {
					document.getElementById('list').style['height'] = $scope.myObj.height;
				}, 300);
				$scope.firstHide = true;
//				console.log(data.order)
			});
		}

		//滑动切换--取消滑动
		$scope.activeSlide = function(type) {
			$scope.input.type = type;
			$scope.input.kind = "";
			$scope.input.goods_name = ""; //商品名称
			$scope.input.goods_bn = ""; //商品编号
			$scope.init_data();
		};
		
		//搜索，是针对全部来搜索的，所以这里$scope.input.type为""
		$scope.doSearch = function(){
			$scope.rightSideMenu();
			$scope.input.type = "";
			$scope.init_data();
		}
		
		//回到顶部
		$scope.doScrollToTop = function(name) {
			$ionicScrollDelegate.$getByHandle(name).scrollTop(true);
		};
		
		//处理订单-通用(个别按钮除外)
		$scope.handleOrder = function(method, order_id) {
			Afallseller.handleOrder(method, order_id, function(data) {
				if(data.success) {
					GoDialog.toast("操作成功！");
					$scope.init_data();
				} else {
					GoDialog.toast(data.msg);
				}
			})
		}
		//确认收款。status=1已收到  status=2未收到
		$scope.confirm = function(order_id, status) {
			Afallseller.confirm(order_id, status, function(data) {
				GoDialog.toast(data.msg);
				if(data.success) {
					$scope.init_data();
				}
				$scope.closeModal_money();//操作完，隐藏弹窗
			})
		}

		//根据按钮的类型，选择对应的事件
		$scope.switchEvent = function(buttonId, index) { //按钮id，同时也要把订单数组的下标传过来，因为其他方法可能要用到订单号、店铺id等信息
			var data = $scope.datalist.order[index];
			switch(buttonId) {
				case 1:
					{ //查看订单
						GoPage.go_states("sellerpayment", {"order_id": data.order_id});
						break;
					};
				case 2:
					{ //取消订单
						$scope.handleOrder("trades.order.cancel", data.order_id);
						break;
					};
				case 3:
					{ //确认收款
//						$scope.confirm($scope.datalist.order[index].order_id);
						$scope.openModal_money(index);//打开弹窗
						break;
					};
				case 4:
					{ //发货：从发货按钮跳转到sellerpayment，需要显示发货输入框
						GoPage.go_states("seller_send", {
							"order_id": data.order_id,
							"buttonId": buttonId
						});
						break;
					};
				case 5:
					{ //延长收货
						$scope.handleOrder("trades.order.extend", data.order_id);
						break;
					};
				case 6:
					{ //修改金额
						GoPage.go_states("afupdate", {
							"order_id": data.order_id
						});
						break;
					};
				case 7:
					{ //处理退款
						break;
					};
				case 8:
					{ //退货、退款
						GoPage.go_states("sellerrefund");
						break;
					};
				case 9:
					{ //收到退货
						break;
					};
				case 10:
					{ //举证
						break;
					};
				case 11:
					{ //上传凭证
						break;
					};
				case 14:
					{ //查看物流
						GoPage.go_states("orderlogistics",{'order_id':data.order_id});
						break;
					};	
			}
		}

		/*----------------上下拉刷新-------------*/
		$scope.isLoad = false;
		$scope.doRefresh = function() {
			$scope.isLoad = true;
			Afallseller.get_list($scope.input.type, 1, $scope.input.kind, $scope.input.goods_bn, $scope.input.goods_name, $scope.input.order_id, $scope.input.time, function(data) {
				if(data != null) {
					$scope.datalist = data;
				} else {
					$scope.datalist = new Array();
				}
				$scope.input.nPage = 1;
			}, function() {
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1000);
			})
		}

		$scope.doRefreshMore = function() {
			if($scope.input.nPage < $scope.tPage) {
				$scope.isLoad = true;
				Afallseller.get_list($scope.input.type, $scope.input.nPage + 1, $scope.input.kind, $scope.input.goods_bn, $scope.input.goods_name, $scope.input.order_id, $scope.input.time, function(data) {
					if(data != null && data.order != null) {
						if($scope.datalist.order != undefined) {
							$scope.datalist.order = $scope.datalist.order.concat(data.order);
							$scope.input.nPage += 1;
						} else {
							$scope.datalist = data;
						}
					}
				}, function() {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1000);
				})
			}
		}
		/*----------------上下拉刷新-------------*/
		
		//滚动侦听
		$scope.doShowTop = function(name, index) {
			var delegate = $ionicScrollDelegate.$getByHandle(name);
			if(delegate.getScrollPosition() == undefined) return;
			var scrollTop = delegate.getScrollPosition().top;
			//分类
			$scope.$apply(function() {
				if($window.innerHeight <= delegate.getScrollPosition().top) {
					$scope.showtop999 = "show";
				} else {
					$scope.showtop999 = "hide";
				}
			});
		};

		/***************侧边菜单*****************/
		//侧边菜单
		$scope.rightSideMenu = function() {
			$scope.hideTile = !$scope.hideTile;
			$ionicSideMenuDelegate.toggleRight();
		};
		$scope.closeRightSideMenu = function(){
			if($ionicSideMenuDelegate.isOpenRight()){
				$scope.hideTile = !$scope.hideTile;
			}
		}
		
		/*****************订单类型****************/
		$scope.menuType = "";
		$scope.typeList = [{code:'1',name:'普通'},{code:'2',name:'秒杀'},{code:'3',name:'团购'},{code:'4',name:'拍卖'},{code:'5',name:'限时抢购'},
		{code:'6',name:'积分兑换'},{code:'7',name:'新品测试'},{code:'8',name:'新品赠送'},{code:'9',name:'新品广告'}]
		$ionicModal.fromTemplateUrl("templates/modal/sellerordertype.html", {
			scope: $scope,
			animation: "slide-in-up"
		}).then(function(modal) {
			$scope.modal_type = modal;
		});
		//打开
		$scope.openModal_type = function() {
			$scope.modal_type.show();
		}
		//隐藏
		$scope.closeModal_type = function() {
			$scope.modal_type.hide();
		}
		//选择订单类型，单选
		$scope.selectTypeId = function(index){
			for (var i=0;i<$scope.typeList.length;i++) {
				if(i!=index){
					$scope.typeList[i].isSelected = false;
				}
			}
			if($scope.typeList[index].isSelected){
				$scope.typeList[index].isSelected = false;
			}else{
				$scope.typeList[index].isSelected = true;
			}
		}
		//得到id
		$scope.getTypeId = function(){
			for (var i=0;i<$scope.typeList.length;i++) {
				if($scope.typeList[i].isSelected){
					$scope.input.kind = $scope.typeList[i].code;
					$scope.menuType = $scope.typeList[i].name;
					break;
				}else{
					$scope.input.kind = "";
					$scope.menuType = "";
				}
			}
			$scope.closeModal_type();
		}
		
		/*****************下单时间****************/
		$scope.menuTime = "";
		var currentYear = new Date().getFullYear();
		$scope.timeList = [{code:'3th',name:'3个月内'},{code:'6th',name:'半年内'},{code:currentYear,name:'今年'}]
		$ionicModal.fromTemplateUrl("templates/modal/sellerordertime.html", {
			scope: $scope,
			animation: "slide-in-up"
		}).then(function(modal) {
			$scope.modal_time = modal;
		});
		//打开
		$scope.openModal_time = function() {
			$scope.modal_time.show();
		}
		//隐藏
		$scope.closeModal_time = function() {
			$scope.modal_time.hide();
		}
		//选择订单类型，单选
		$scope.selectTimeId = function(index){
			for (var i=0;i<$scope.timeList.length;i++) {
				if(i!=index){
					$scope.timeList[i].isSelected = false;
				}
			}
			if($scope.timeList[index].isSelected){
				$scope.timeList[index].isSelected = false;
			}else{
				$scope.timeList[index].isSelected = true;
			}
		}
		//得到id
		$scope.getTimeId = function(){
			for (var i=0;i<$scope.timeList.length;i++) {
				if($scope.timeList[i].isSelected){
					$scope.input.time = $scope.timeList[i].code;
					$scope.menuTime = $scope.timeList[i].name;
					break;
				}else{
					$scope.input.time = "";
					$scope.menuTime = "";
				}
			}
			$scope.closeModal_time();
		}
		
		
		/*****************确认收款****************/
		$ionicModal.fromTemplateUrl("templates/modal/confirmMoney.html", {
			scope: $scope,
			animation: "slide-in-up"
		}).then(function(modal) {
			$scope.modal_money = modal;
		});
		//打开
		$scope.openModal_money = function(index) {
			var data = $scope.datalist.order[index];
			if(!data||!data.payment_info) return;
			$scope.payInfo = data.payment_info;
			$scope.payInfo.order_id = data.order_id;
			$scope.modal_money.show();
		}
		//隐藏
		$scope.closeModal_money = function() {
			$scope.modal_money.hide();
		}

	}

	controller.$inject = ['$scope', 'Afallseller', 'GoPage', 'GoDialog', '$timeout', '$stateParams', 'Auth', '$window', '$ionicScrollDelegate','$ionicSideMenuDelegate','$ionicModal'];

	return app.registerController("afallsellerctrl",controller);
});